home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / apps / RawtoILBM / RawtoILBM.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  4KB  |  177 lines

  1. /* RawtoILBM
  2.  * Converts raw file (from ILBMtoRaw) into an ILBM
  3.  * Requires linkage with several iffparse modiules - See Makefile
  4.  * 37.9 04/92
  5.  */ 
  6. #define INTUI_V36_NAMES_ONLY
  7.  
  8. #include "iffp/ilbmapp.h"
  9.  
  10. #include <intuition/intuitionbase.h>
  11. #include <workbench/workbench.h>
  12.  
  13.  
  14. #ifdef __SASC
  15. void __chkabort(void) {}          /* Disable SAS CTRL-C checking. */
  16. #else
  17. #ifdef LATTICE
  18. void chkabort(void) {}            /* Disable LATTICE CTRL-C checking */
  19. #endif
  20. #endif
  21.  
  22.  
  23. #include "rawtoilbm_rev.h"
  24. UBYTE vers[] = VERSTAG;
  25. UBYTE Copyright[] = VERS " - Convert raw file to ILBM - Freely Redistributable";
  26.  
  27. #define MINARGS 6
  28. char *usage = "Usage: RawtoILBM rawname ilbmname width height depth\n";
  29.  
  30. void bye(UBYTE *s,int e);
  31. void cleanup(void);
  32.  
  33. struct Library    *IntuitionBase = NULL;
  34. struct Library    *GfxBase = NULL;
  35. struct Library    *IFFParseBase = NULL;
  36.  
  37. struct ILBMInfo ilbm = {0};
  38.  
  39. /* Max color registers for this exmple */
  40. #define MAXAMCOLORREG 32
  41.  
  42. USHORT    colortable[MAXAMCOLORREG];
  43.  
  44. BOOL fromWB;
  45.  
  46.  
  47. void main(int argc, char **argv)
  48.     {
  49.     LONG    error = 0L, rawfile, rlen;
  50.     USHORT    width, height, depth, pwidth, pheight, pmode, extra;
  51.     ULONG     plsize;
  52.     char        *rawname,*ilbmname;
  53.     int     k;
  54.  
  55.     fromWB = (argc==0) ? TRUE : FALSE;
  56.  
  57.     if(!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  58.       bye("Can't open intuition library.\n",RETURN_WARN);
  59.       
  60.     if(!(GfxBase = OpenLibrary("graphics.library",0)))
  61.       bye("Can't open graphics library.\n",RETURN_WARN);
  62.  
  63.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  64.       bye("Can't open iffparse library.\n",RETURN_WARN);
  65.  
  66.     if(!(ilbm.ParseInfo.iff = AllocIFF()))
  67.       bye(IFFerr(IFFERR_NOMEM),RETURN_WARN);
  68.  
  69.     if(argc==MINARGS)                 /* Passed filenames via command line  */
  70.           {
  71.           rawname  = argv[1];
  72.           ilbmname = argv[2];
  73.     width  = atoi(argv[3]);
  74.     height = atoi(argv[4]);
  75.     depth  = atoi(argv[5]);
  76.  
  77.     /* Page width, height, and mode for saved ILBM */
  78.     pwidth  = width  < 320 ? 320 : width;
  79.     pheight = height < 200 ? 200 : height;
  80.     pmode    = pwidth >= 640  ? HIRES : 0L;
  81.     pmode  |= pheight >= 400 ? LACE  : 0L;
  82.  
  83.     plsize = RASSIZE(width,height);
  84.     }
  85.     else
  86.     {
  87.     printf("%s\n%s\n",Copyright,usage);
  88.     bye("\n",RETURN_OK);
  89.     }
  90.      
  91.  
  92.     if(!(rawfile = Open(rawname,MODE_OLDFILE)))
  93.     {
  94.     printf("Can't open raw file '%s'\n",rawname);
  95.     bye(" ",RETURN_WARN);
  96.     }
  97.  
  98.     /*
  99.      * Allocate Bitmap and planes
  100.      */
  101.      extra = depth > 8 ? depth - 8 : 0;
  102.      if(ilbm.brbitmap = AllocMem(sizeof(struct BitMap) + (extra<<2),
  103.                 MEMF_CLEAR))
  104.         {
  105.         InitBitMap(ilbm.brbitmap,depth,width,height);
  106.         for(k=0, error=0, rlen=1; k<depth && (!error) && (rlen >0); k++) 
  107.             {
  108.             if(!(ilbm.brbitmap->Planes[k] = AllocRaster(width,height)))
  109.             error = IFFERR_NOMEM;
  110.             if(! error)
  111.         {
  112.                 BltClear(ilbm.brbitmap->Planes[k], RASSIZE(width,height),0);
  113.         /* Read a plane */
  114.         rlen = Read(rawfile,ilbm.brbitmap->Planes[k],plsize);
  115.                 }
  116.         }
  117.  
  118.     /* get colortable */
  119.     if((!error)&&(rlen > 0))
  120.         rlen=Read(rawfile,colortable,(MIN(1<<depth,MAXAMCOLORREG)<<1));
  121.  
  122.         if((error)||(rlen<=0))
  123.             {
  124.         if(rlen <= 0)      printf("Error loading raw file - check dimensions\n");
  125.             else         printf("Error allocating planes\n");
  126.         }
  127.     else
  128.         {
  129.         error = saveilbm(&ilbm, ilbm.brbitmap, pmode,
  130.                 width,  height, pwidth, pheight,
  131.                 colortable, MIN(1<<depth,MAXAMCOLORREG), 4,    /* colors */ 
  132.         mskNone, 0,        /* masking. transColor */
  133.         NULL, NULL,        /* additional chunk lists */
  134.         ilbmname);
  135.         }
  136.  
  137.         for(k=0; k<depth; k++) 
  138.             {
  139.             if(ilbm.brbitmap->Planes[k])
  140.             FreeRaster(ilbm.brbitmap->Planes[k],width,height);
  141.         }
  142.     FreeMem(ilbm.brbitmap, sizeof(struct BitMap) + (extra << 2));
  143.     }
  144.  
  145.     Close(rawfile);
  146.  
  147.     if(error)
  148.     {
  149.     printf("%s\n",IFFerr(error));
  150.     bye(" ", RETURN_FAIL);
  151.     }
  152.     else bye("",RETURN_OK);
  153.     }
  154.  
  155.  
  156. void bye(UBYTE *s,int e)
  157.     {
  158.     if(s&&(*s)) printf("%s\n",s);
  159.     if ((fromWB)&&(*s))    /* Wait so user can read messages */
  160.         {
  161.         printf("\nPRESS RETURN TO EXIT\n");
  162.         while(getchar() != '\n');
  163.         }
  164.     cleanup();
  165.     exit(e);
  166.     }
  167.  
  168. void cleanup()
  169.     {
  170.     if(ilbm.ParseInfo.iff)    FreeIFF(ilbm.ParseInfo.iff);
  171.  
  172.     if(GfxBase)        CloseLibrary(GfxBase);
  173.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  174.     if(IFFParseBase)    CloseLibrary(IFFParseBase);
  175.     }
  176.  
  177.